package com.tan.lbs.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.tan.lbs.dao.LocationDAO;
import com.tan.lbs.entity.Item;
import com.tan.lbs.entity.Location;
import com.tan.lbs.util.HibernateUtil;

public class LocationDAOImpl implements LocationDAO {

	@Override
	public String create(Location location) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(location);
		s.getTransaction().commit();
		return location.getLocationID();
	}

	@Override
	public void update(Location location) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.update(location);
		s.getTransaction().commit();
		
	}

	@Override
	public Location readByID(String locationID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Location location = (Location)s.get(Location.class, locationID);
		s.getTransaction().commit();
		return location;
		
	}

	@Override
	public void delete(String locationID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Location location = new Location();
		location.setLocationID(locationID);
		s.delete(location);
		s.getTransaction().commit();		
	}

}
